#include "OgreURLPlugin.h"

#include <OgreURLArchiveFactory.h>

#include <OgreException.h>
#include <OgreArchiveManager.h>

namespace Ogre
{

//-------------------------------------------------------------------------------------------------------------------------------------------------
URLPlugin::URLPlugin()
: mURLArchiveFactory(NULL)
{
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
URLPlugin::~URLPlugin()
{
	OgreAssert( !mURLArchiveFactory, "Application bug. " 
		"The factory for URL archive must be destroyed "
		"before URLPlugin." );
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
const String& URLPlugin::getName() const
{
	static const String sPluginName = "URL";
	return sPluginName;
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
void URLPlugin::install()
{
	OgreAssert( !mURLArchiveFactory, "Application bug. URL plugin is already installed." );

	// Create new factory instance
	mURLArchiveFactory = OGRE_NEW_T( URLArchiveFactory, MEMCATEGORY_GENERAL )();

	// Register URL archive factory
	ArchiveManager::getSingleton().addArchiveFactory( mURLArchiveFactory );
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
void URLPlugin::initialise()
{
	assert( mURLArchiveFactory && "Application bug. URL plugin not installed." );

	// Is factory instance exist?
	if( !mURLArchiveFactory )
	{
		OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
			"Application bug, plugin is not installed",
			"URLPlugin::initialise" );
	}
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
void URLPlugin::shutdown()
{
	assert( mURLArchiveFactory && "Application bug. URL plugin not installed." );

	// Is factory instance exist?
	if( !mURLArchiveFactory )
	{
		OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
			"Application bug, plugin is not installed",
			"URLPlugin::initialise" );
	}
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
void URLPlugin::uninstall()
{
	OgreAssert( mURLArchiveFactory, "Application bug. URL plugin not installed." );

	// Destroy existing instance
	OGRE_DELETE_T( mURLArchiveFactory, URLArchiveFactory, MEMCATEGORY_GENERAL );
	mURLArchiveFactory = NULL;
}

} // end of namespace Ogre
